package com.bookstore.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.UnsupportedEncodingException;

public class EncodingHttpServletRequest extends HttpServletRequestWrapper {
    private HttpServletRequest request;
    private String encoding;

    public EncodingHttpServletRequest(HttpServletRequest request, String encoding) {
        super(request);
        this.request = request;
        this.encoding = encoding;
    }

    @Override
    public String getParameter(String name) {
        //通过原本 request 对象的 getParameter 方法拿到 value
        String value = request.getParameter(name);
        //
        try {
            value = new String(value.getBytes("UTF-8"), encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return value;
    }
}
